import router from "./router";

import store from "./store";


router.beforeEach((to, from, next)=>{
    let token = store.state.token

    if(token){
        if(to.path == '/login'){
            next(from.path)
        }else{
            // next();
            if(goToPath(to.path)){
                next()
            }else{
                next('/404')
            }
        }
    }else{
        if(to.path == '/login'){
            next()
        }else{
            next('/login');
        }
    }
})


function goToPath(path){
    // 获取到所有路由
    let allRouter = router.getRoutes();

    let routers = allRouter.filter((item, index)=>{
        return item.path == path
    })

    if(routers.length > 0){
        return true
    }else{
        return false
    }
}